/*
package com.thread.threadfirst.rabbitMQPublisher.config;

import com.thread.threadfirst.rabbitMQPublisher.constant.AMQPConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import java.util.HashMap;
import java.util.Map;

*/
/**
 * 项目启动就会自动在rabbitMQ中注入配置的exchange、queue 和它们的绑定关系。
 *//*

@Configuration
public class AMQPConfig {

    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;

    @Autowired
    private Environment env;

    @Autowired
    private SimpleRabbitListenerContainerFactoryConfigurer simpleRabbitListenerContainerFactoryConfigurer;

//    @Bean(name = AMQPConstant.QUEUE_FIRST)
//    public Queue queue1(){
//        return new Queue(AMQPConstant.QUEUE_FIRST);
//    }
//
    //durable 默认为true
//    @Bean(name = AMQPConstant.QUEUE_SECOND)
//    public Queue queue2(){
//        return new Queue(AMQPConstant.QUEUE_SECOND);
//    }
//
//    @Bean(name = AMQPConstant.QUEUE_THIRD)
//    public Queue queue3(){
//        return new Queue(AMQPConstant.QUEUE_THIRD);
//    }
//
    //durable 默认为true，autoDelete 默认为false
//    @Bean(name = AMQPConstant.EXCHANGE_TOPIC_FIRST)
//    public TopicExchange exchange1(){
//        return new TopicExchange(AMQPConstant.EXCHANGE_TOPIC_FIRST,true,false);
//    }
//
//    @Bean(name = AMQPConstant.EXCHANGE_FANOUT_FIRST)
//    public FanoutExchange exchange2(){
//        return new FanoutExchange(AMQPConstant.EXCHANGE_FANOUT_FIRST);
//    }
//
//    @Bean(name = AMQPConstant.ROUTINGKEY_FIRST)
//    public Binding bindingExchange1(@Qualifier(AMQPConstant.QUEUE_FIRST) Queue queue,
//                                   @Qualifier(AMQPConstant.EXCHANGE_TOPIC_FIRST) TopicExchange topicExchange){
//        return BindingBuilder.bind(queue).to(topicExchange).with(AMQPConstant.ROUTINGKEY_FIRST);
//    }
//
//    @Bean(name = AMQPConstant.ROUTINGKEY_SECOND)
//    public Binding bindingExchange2(@Qualifier(AMQPConstant.QUEUE_SECOND) Queue queue,
//                                   @Qualifier(AMQPConstant.EXCHANGE_TOPIC_FIRST) TopicExchange topicExchange){
//        return BindingBuilder.bind(queue).to(topicExchange).with(AMQPConstant.ROUTINGKEY_SECOND);
//    }
//
//    @Bean
//    public Binding bindingExchange3(@Qualifier(AMQPConstant.QUEUE_SECOND) Queue queue,
//                                    @Qualifier(AMQPConstant.EXCHANGE_FANOUT_FIRST) FanoutExchange fanoutExchange){
//        return BindingBuilder.bind(queue).to(fanoutExchange);
//    }
//
//    @Bean
//    public Binding bindingExchange4(@Qualifier(AMQPConstant.QUEUE_THIRD) Queue queue,
//                                    @Qualifier(AMQPConstant.EXCHANGE_FANOUT_FIRST) FanoutExchange fanoutExchange){
//        return BindingBuilder.bind(queue).to(fanoutExchange);
//    }

    private static final String DEAD_LETTER_EXCHANGE = "x-dead-letter-exchange";
    private static final String DEAD_LETTER_ROUTING_KEY = "x-dead-letter-routing-key";

    @Bean(name = AMQPConstant.DL_EXCHANGE)
    public DirectExchange dlExchange(){
        return new DirectExchange(AMQPConstant.DL_EXCHANGE);
    }

    @Bean(name = AMQPConstant.DL_QUEUE)
    public Queue dlQueue(){
        Map<String, Object> args = new HashMap<>(2);
        args.put(DEAD_LETTER_EXCHANGE,AMQPConstant.DL_EXCHANGE);
        args.put(DEAD_LETTER_ROUTING_KEY,"dl_r");
        return new Queue(AMQPConstant.DL_QUEUE,true,false,false,args);
    }

    @Bean(name = AMQPConstant.REDIRECT_QUEUE)
    public Queue redirectQueue(){
        return new Queue(AMQPConstant.REDIRECT_QUEUE);
    }

    @Bean(name = "dl_r")
    public Binding bindingDLExchange(@Qualifier(AMQPConstant.REDIRECT_QUEUE)Queue redirectQueue,
                                     @Qualifier(AMQPConstant.DL_EXCHANGE)DirectExchange dlExchange){
        return BindingBuilder.bind(redirectQueue).to(dlExchange).with("dl_r");
    }

    @Bean(name = "dl_k")
    public Binding bindingDLExchange(@Qualifier(AMQPConstant.DL_QUEUE)Queue dlQueue,
                                     @Qualifier(AMQPConstant.EXCHANGE_TOPIC_FIRST)TopicExchange topicExchange){
        return BindingBuilder.bind(dlQueue).to(topicExchange).with("dl_k");
    }


    */
/**
     * 管理 RabbitMQ监听器listener 的容器工厂
     * 单一消费者
     *
     * @return
     *//*

    @Bean
    public SimpleRabbitListenerContainerFactory singleListenerContainer() {
        SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
        containerFactory.setConnectionFactory(cachingConnectionFactory);
        containerFactory.setMessageConverter(new Jackson2JsonMessageConverter());  //消息序列化方式
        containerFactory.setConcurrentConsumers(1);  //当前消费者数
        containerFactory.setMaxConcurrentConsumers(1);  //最大消费者数
        containerFactory.setPrefetchCount(1);  //每个消费者一次从队列中获取几个消息
//        containerFactory.setTxSize(1);
        containerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);  //手动ack
        //待查询！
//        directRabbitListenerContainerFactory.setConsumersPerQueue(Runtime.getRuntime().availableProcessors());
//        directRabbitListenerContainerFactory.setConsumersPerQueue(10);
        return containerFactory;
    }

    */
/**
     * 多个消费者
     *
     * @return
     *//*

    @Bean
    public SimpleRabbitListenerContainerFactory multiListenerContainer() {
        SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
        simpleRabbitListenerContainerFactoryConfigurer.configure(containerFactory, cachingConnectionFactory);
        containerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        containerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
        containerFactory.setConcurrentConsumers(env.getProperty("spring.rabbitmq.listener.simple.concurrency", int.class));
        containerFactory.setMaxConcurrentConsumers(env.getProperty("spring.rabbitmq.listener.simple.max-concurrency", int.class));
        containerFactory.setPrefetchCount(env.getProperty("spring.rabbitmq.listener.simple.prefetch", int.class));
        //待查询！
//        containerFactory.setTaskExecutor(getTaskExecutor());
        return containerFactory;
    }

//    @Bean
//    public SimpleAsyncTaskExecutor getTaskExecutor() {
//        return new SimpleAsyncTaskExecutor("marvel-consumer-");
//    }
}
*/
